#pragma once
#include <mariadb/mysql.h>

namespace ns_mysql
{
    using namespace ns_log;

    const std::string HOST = "192.168.74.128";
    const std::string USER = "oj_client";
    const std::string PASSWD = "123456";
    const std::string DBNAME = "oj";

    bool MySQLInit(MYSQL *&mysql, const std::string &host=HOST, const std::string &user=USER, const std::string &password=PASSWD, const std::string &database=DBNAME)
    {
        mysql = mysql_init(nullptr); // 初始化MYSQL对象
        if (mysql == nullptr)
        {
            std::cerr << "mysql_init() failed" << std::endl;
            return false;
        }

        // 连接到数据库
        if (mysql_real_connect(mysql, host.c_str(), user.c_str(), password.c_str(), database.c_str(), 0, nullptr, 0) == nullptr)
        {
            std::cerr << "mysql_real_connect() failed: " << mysql_error(mysql) << std::endl;
            mysql_close(mysql);
            return false;
        }

        std::cout << "Connected to the database successfully!" << std::endl;

        // 这里可以执行其他数据库操作

        mysql_set_character_set(mysql, "utf8");
        return true;
    }
}
